<?php
/**
 * CDN直播频道队列管理
 * @author liangpan
 */
include_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'public.class.php';
class nl_cdn_live_media_queue extends nl_public
{
	static $base_table='nns_cdn_live_media_queue';
	static $arr_filed = array(
			'nns_id',
			'nns_name',
			'nns_live_id',
			'nns_live_index_id',
			'nns_live_media_id',
			'nns_action',
			'nns_import_cdn_state',
			'nns_import_cdn_time',
			'nns_import_cms_state',
			'nns_import_cms_time',
			'nns_import_notify_state',
			'nns_import_notify_time',
			'nns_message_id',
			'nns_cp_id',
			'nns_create_time',
			'nns_modify_time',
	);
	
	/**
	 * 查询单个列表
	 * @param object $dc 数据库对象
	 * @param string $nns_id guid
	 * @return Ambigous array('ret'=>'状态码','reason'=>'原因','data'=>'数据')
	 * @author liangpan
	 * @date 2016-09-24
	 */
	static public function query_by_id($dc,$nns_id)
	{
		$sql = "select * from " . self::$base_table . " where nns_id='{$nns_id}' limit 1";
		$result = nl_query_by_db($sql, $dc->db());
		if(!$result)
		{
			return self::return_data(1,'数据库查询失败,sql:'.$sql);
		}
		$result = (isset($result[0]) && !empty($result[0])) ? $result[0] : null;
		return self::return_data(0,'OK',$result);
	}
	
	
	/**
	 * 添加CDN直播频道队列
	 * @param object $dc 数据库对象
	 * @param array $params 数据数组
	 * @return Ambigous <array('ret'=>'状态码','reason'=>'原因','data'=>'数据')
	 * @author liangpan
	 * @date 2016-09-24
	 */
	static public function add($dc,$params)
	{
		$params = self::make_nns_pre($params);
		if(!isset($params['nns_id']) || strlen($params['nns_id'])<1)
		{
			$params['nns_id'] = np_guid_rand();
		}
		$date_time = date("Y-m-d H:i:s");
		if(!isset($params['nns_create_time']) || strlen($params['nns_create_time'])<1)
		{
			$params['nns_create_time'] = $date_time;
		}
		if(!isset($params['nns_modify_time']) || strlen($params['nns_modify_time'])<1)
		{
			$params['nns_modify_time'] = $date_time;
		}
		$params = self::except_useless_params(self::$arr_filed, $params);
		if(empty($params))
		{
			return self::return_data(1,'参数为空');
		}
		$sql = self::make_insert_sql(self::$base_table, $params);
		$result = nl_execute_by_db($sql, $dc->db());
		if(!$result)
		{
			return self::return_data(1,'数据库执行失败'.$sql);
		}
		return self::return_data(0,'ok');
	}
	
	/**
	 * 修改CDN直播频道队列
	 * @param object $dc 数据库对象
	 * @param array $params 数据数组
	 * @param string $nns_id GUID
	 * @return Ambigous <array('ret'=>'状态码','reason'=>'原因','data'=>'数据')
	 * @author liangpan
	 * @date 2016-09-24
	 */
	static public function edit($dc,$params,$nns_id)
	{
		$params = self::make_nns_pre($params);
		if(strlen($nns_id)<0)
		{
			return self::return_data(1,'CDN直播频道队列guid为空');
		}
		$params = self::except_useless_params(self::$arr_filed, $params);
		if(empty($params))
		{
			return self::return_data(1,'参数为空');
		}
		if(!isset($params['nns_modify_time']) || strlen($params['nns_modify_time'])<1)
		{
			$params['nns_modify_time'] = date("Y-m-d H:i:s");
		}
		$sql = self::make_update_sql(self::$base_table, $params,array('nns_id'=>$nns_id));
		$result = nl_execute_by_db($sql, $dc->db());
		if(!$result)
		{
			return self::return_data(1,'数据库执行失败'.$sql);
		}
		return self::return_data(0,'ok');
	}

	
	/**
	 * 查询CDN直播频道队列列表
	 * @param object $dc 数据库对象
	 * @param array $params 查询参数
	 * @param array $page_info 分页信息
	 * @return Ambigous array('ret'=>'状态码','reason'=>'原因','data'=>'数据')
	 * @author liangpan
	 * @date 2016-09-24
	 */
	static public function query($dc,$params,$page_info=null)
	{
		if(isset($params['where']) && !empty($params['where']))
		{
			foreach ($params['where'] as $where_key=>$where_val)
			{
				if($where_key == 'create_begin_time')
				{
					self::$str_where.=" nns_create_time >= '{$where_val}' and ";
				}
				else if($where_key == 'create_begin_time')
				{
					self::$str_where.=" create_end_time <= '{$where_val}' and ";
				}
				else
				{
					self::$str_where.=" {$where_key} = '{$where_val}' and ";
				}
			}	
		}
		if(isset($params['like']) && !empty($params['like']))
		{
			foreach ($params['like'] as $like_key=>$like_val)
			{
				self::$str_where.=" {$like_key} like '%{$like_val}%' and ";
			}
		}
		self::$str_where = (strlen(self::$str_where) > 5) ? trim(self::$str_where,'and ') : '';
		$str_limit = self::make_page_limit($page_info);
		$sql="select * from " . self::$base_table . " " . self::$str_where . " order by nns_create_time desc {$str_limit} ";
		$sql_count = "select count(*) as count from " . self::$base_table . " " . self::$str_where;
		$result_count = nl_query_by_db($sql_count,  $dc->db());
		$result = nl_query_by_db($sql, $dc->db());
		self::init_where();
		if(!$result || !$result_count)
		{
			return self::return_data(1,"查询数据失败".$sql);
		}
		return self::return_data(0,"查询数据成功",$result,$result_count);
	}
	
	/**
	 * 查询唯一的队列数据
	 * @param unknown $dc
	 * @param unknown $live_id
	 * @param unknown $live_index_id
	 * @param unknown $live_media_id
	 * @param unknown $cp_id
	 * @return Ambigous <array('ret'=>'状态码','reason'=>'原因','data'=>'数据'), multitype:NULL number string , number, mixed, multitype:NULL multitype:a r y s t i n g  array multitype:number  >
	 */
	static public function query_queue_is_exsist($dc,$live_id,$live_index_id,$live_media_id,$cp_id)
	{
		if(strlen($live_id) <1)
		{
			return self::return_data(1,"直播ID为空：".$live_id);
		}
		if(strlen($live_index_id) <1)
		{
			return self::return_data(1,"直播分集ID为空：".$live_index_id);
		}
		if(strlen($live_media_id) <1)
		{
			return self::return_data(1,"直播片源ID为空：".$live_media_id);
		}
		if(strlen($cp_id) <1)
		{
			return self::return_data(1,"直播分集ID为空：".$cp_id);
		}
		$sql="select * from " . self::$base_table . " where nns_cp_id='{$cp_id}' and nns_live_id='{$live_id}' and nns_live_index_id='{$live_index_id}' and nns_live_media_id='{$live_media_id}' limit 1";
		$result = nl_query_by_db($sql, $dc->db());
		$result = (isset($result[0]) && !empty($result[0])) ? $result[0] : null;
		return self::return_data(0,'OK',$result);
	}
	
	
	
	/**
	 * 删除
	 * @param object $dc 数据库对象
	 * @param string $nns_id GUID
	 * @return Ambigous array('ret'=>'状态码','reason'=>'原因','data'=>'数据')
	 * @author liangpan
	 * @date 2016-09-24
	 */
	static public function delete($dc,$mix_id)
	{
		if(empty($mix_id) || (!is_array($mix_id) && !is_string($mix_id)))
		{
			return self::return_data(1,'CDN直播频道队列_id为空,或则参数错误：'.var_export($mix_id));
		}
		$str_id= (is_array($mix_id)) ? implode("','", $mix_id) : $mix_id;
		$sql = "delete from " . self::$base_table . " where nns_id in ('{$str_id}') ";
		$result = nl_execute_by_db($sql, $dc->db());
		if(!$result)
		{
			return self::return_data(1,'数据库查询失败,sql:'.$sql);
		}
		return self::return_data(0,'OK');
	}
	
	/**
	 * 查询等待CDN注入的消息队列
	 * @param object $dc 数据库对象
	 * @param string $cp_id CP_ID
	 * @param number $cdn_state CDN注入状态 0 等待注入CDN |  1 正在注入CDN | 2 注入CDN 成功 | 3 注入CDN失败
	 * @param number $queue_num 查询数量
	 * @return Ambigous <array('ret'=>'状态码','reason'=>'原因','data'=>'数据')
	 * @author liangpan
	 * @date 2016-09-24
	 */
	static public function query_wait_cdn_queue($dc,$cp_id,$cdn_state=0,$queue_num=10)
	{
		if(strlen($cp_id) <1)
		{
			return self::return_data(1,"CPID为空".var_export($cp_id,true));
		}
		$sql="select * from " . self::$base_table . " where nns_cp_id='{$cp_id}' and nns_import_cdn_state='{$cdn_state}' order by nns_modify_time asc limit {$queue_num} ";
		$result = nl_query_by_db($sql, $dc->db());
		if(!$result)
		{
			return self::return_data(1,"查询数据失败".$sql);
		}
		return self::return_data(0,"查询数据成功",$result);
	}
	
	
	/**
	 * 查询等待CMS注入的消息队列
	 * @param object $dc 数据库对象
	 * @param string $cp_id CP_ID
	 * @param number $cms_state CDN注入状态 0 等待注入CMS |  1 正在注入CMS | 2 注入CMS 成功 | 3 注入CMS失败
	 * @param number $queue_num 查询数量
	 * @return Ambigous <array('ret'=>'状态码','reason'=>'原因','data'=>'数据')
	 * @author liangpan
	 * @date 2016-09-24
	 */
	static public function query_wait_cms_queue($dc,$cp_id,$cms_state=0,$queue_num=10)
	{
		if(strlen($cp_id) <1)
		{
			return self::return_data(1,"CPID为空".var_export($cp_id,true));
		}
		$sql="select * from " . self::$base_table . " where nns_cp_id='{$cp_id}' and nns_import_cms_state='{$cms_state}' order by nns_modify_time asc limit {$queue_num} ";
		$result = nl_query_by_db($sql, $dc->db());
		if(!$result)
		{
			return self::return_data(1,"查询数据失败".$sql);
		}
		return self::return_data(0,"查询数据成功",$result);
	}
	
	/**
	 * 查询消息响应注入的消息队列
	 * @param object $dc 数据库对象
	 * @param string $cp_id CP_ID
	 * @param number $notify_state 消息响应注入状态 0 等待响应 上游 |  1 正在响应上游 | 2 响应上游 成功 | 3 响应上游失败
	 * @param number $queue_num 查询数量
	 * @return Ambigous <array('ret'=>'状态码','reason'=>'原因','data'=>'数据')
	 * @author liangpan
	 * @date 2016-09-24
	 */
	static public function query_wait_notify_queue($dc,$cp_id,$notify_state=0,$queue_num=10)
	{
		if(strlen($cp_id) <1)
		{
			return self::return_data(1,"CPID为空".var_export($cp_id,true));
		}
		$sql="select * from " . self::$base_table . " where nns_cp_id='{$cp_id}' and nns_import_cms_state='{$notify_state}' order by nns_modify_time asc limit {$queue_num} ";
		$result = nl_query_by_db($sql, $dc->db());
		if(!$result)
		{
			return self::return_data(1,"查询数据失败".$sql);
		}
		return self::return_data(0,"查询数据成功",$result);
	}
}
